﻿@{
    Layout = "~/Views/Shared/_LayoutUser.cshtml";
    ViewBag.Title = "网非e学院--修改密码";
}

<form method="post" id="formPwd">
    <div class="weui_cell">
    </div>
    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">手机号：</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="text" pattern="[0-9]*" readonly="readonly" value="@ViewBag.Phone" id="phone" name="Phone">
            <input name="UserID" type="hidden" value="@ViewBag.UserID" />
        </div>
    </div>

    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">新密码：</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="password" placeholder="新密码" id="pwd" name="Pwd">
        </div>
    </div>

    <div class="weui_cell">
        <div class="weui_cell_hd"><label class="weui_label">短信验证码：</label></div>
        <div class="weui_cell_bd weui_cell_primary">
            <input class="weui_input" type="text" placeholder="验证码" id="mobileCode" name="MobileCode">
            <span class="weui_btn weui_btn_plain_primary btnSend" onclick="SendCode()" id="btnSend">发送</span>
        </div>
    </div>
    <div class="weui_cell">
    </div>
    <div>
        <div id="ErrorInfo" class="error">
            @ViewBag.ErrorInfo
        </div>
        <span class="customBtn" onclick="UpdatePwd()">修改</span>
    </div>
</form>

<script>
    var curCount = 0;
    //发送短信验证码
    function SendCode(phone) {
        var phone = $("#phone").val().replace(/\s/g, "");
        if (phone.length != 11) {
            $("#ErrorInfo").html("请输入正确的手机号！");
            return -1;
        }
        else {
            $("#ErrorInfo").html("");
        }

        curCount = 100;
        $("#btnSend").removeAttr("onclick");
        $("#btnSend").removeClass("weui_btn_plain_primary");
        $("#btnSend").addClass("weui_btn_plain_default");
        $("#btnSend").html(curCount);

        $.ajax({
            type: "Post", //提交方式
            url: "../User/SendValiCode",//获取数据的函数
            data: { "phone": phone }, //参数
            error: function () { $("#ErrorInfo").html("验证码发送失败，请重试！"); },
            success: function (data) {
                if (data == "1") {
                    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
                }
            }
        });
    }


    //倒计时
    function SetRemainTime() {
        if (curCount <= 0) {
            window.clearInterval(InterValObj);//停止计时器
            $("#btnSend").attr("onclick", "SendCode()");
            $("#btnSend").html("发送");
            $("#btnSend").removeClass("weui_btn_plain_default ");
            $("#btnSend").addClass("weui_btn_plain_primary");
        }
        else {
            curCount--;
            $("#btnSend").html(curCount);
        }
    }

    //修改密码
    function UpdatePwd() {
        var pwd = $("#pwd").val();
        var code = $("#mobileCode").val();
        if (pwd.length > 20 || pwd.length < 6) {
            $("#ErrorInfo").html("请输入6-14位的新密码！");
            return;
        }
        else {
            $("#ErrorInfo").html("");
        }

        if (code == "" || code == null) {
            $("#ErrorInfo").html("请输入短信验证码！");
            return;
        }
        else {
            $("#ErrorInfo").html("");
        }

        //密码 MD5加密
        $("#pwd").val($.md5($("#pwd").val()));
        $("#formPwd").submit();
    }
</script>
